﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;

namespace WFL.Windows
{
    public static class API
    {
        public static class Kernel32
        {
            [DllImport("Kernel32.dll", EntryPoint = "GetLastError", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetLastError();

            [DllImport("Kernel32.dll", EntryPoint = "SetLastError", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern void SetLastError(int dwErrCode);

            [DllImport("Kernel32.dll", EntryPoint = "GetCurrentThreadId", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetCurrentThreadId();


        }

        public static class User32
        {
            [DllImport("User32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, uint flags);

            [DllImport("User32.dll", EntryPoint = "ReleaseDC", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

            [DllImport("User32.dll", EntryPoint = "GetWindowDC", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);

            [DllImport("User32.dll", EntryPoint = "GetUpdateRgn", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetUpdateRgn(IntPtr hWnd, out IntPtr hRgn, int bErase);

            [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

            [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref IntPtr lParam);

            [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, ref IntPtr wParam, ref IntPtr lParam);

            [DllImport("User32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

            [DllImport("User32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

            [DllImport("User32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

            [DllImport("User32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

            [DllImport("User32.dll", EntryPoint = "GetParent", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetParent(IntPtr hWnd);

            [DllImport("User32.dll", EntryPoint = "GetParent", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);

            [DllImport("User32.dll", EntryPoint = "SetWindowsHookEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SetWindowsHookEx(int idHook, Delegate lpfn, IntPtr hMod, int dwThreadId);

            [DllImport("User32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

            [DllImport("User32.dll", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("User32.dll", EntryPoint = "GetForegroundWindow", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetForegroundWindow();

            [DllImport("User32.dll", EntryPoint = "GetFocus", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetFocus();

            [DllImport("User32.dll", EntryPoint = "GetClassName", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetClassName(IntPtr hWnd, [MarshalAs(UnmanagedType.LPTStr)]StringBuilder lpClassName, int nMaxCount);

            [DllImport("User32.dll", EntryPoint = "GetWindowThreadProcessId", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

            [DllImport("User32.dll", EntryPoint = "AttachThreadInput", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int AttachThreadInput(int idAttach, int idAttachTo, [MarshalAs(UnmanagedType.Bool)]bool fAttach);

            [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

            [DllImport("User32.dll", EntryPoint = "MapVirtualKey", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern uint MapVirtualKey(uint uCode, uint uMapType);

            [DllImport("User32.dll", EntryPoint = "GetAsyncKeyState", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern short GetAsyncKeyState(int vKey);

            [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern short GetKeyState(int nVirtKey);

            [DllImport("User32.dll", EntryPoint = "RegisterHotKey", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

            [DllImport("User32.dll", EntryPoint = "UnregisterHotKey", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int UnregisterHotKey(IntPtr hWnd, int id);

            [DllImport("User32.dll", EntryPoint = "GetCaretPos", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetCaretPos(out POINT lpPoint);

            [DllImport("User32.dll", EntryPoint = "UpdateWindow", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int UpdateWindow(IntPtr hWnd);

            [DllImport("User32.dll", EntryPoint = "IsWindow", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int IsWindow(IntPtr hWnd);

            [DllImport("User32.dll", EntryPoint = "ScrollWindowEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, ref IntPtr prcUpdate, int flags);

            [DllImport("User32.dll", EntryPoint = "SetWindowPos", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

            [DllImport("User32.dll", EntryPoint = "SetCapture", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr SetCapture(IntPtr hWnd);

            [DllImport("User32.dll", EntryPoint = "ReleaseCapture", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int ReleaseCapture();

            [DllImport("User32.dll", EntryPoint = "GetSystemMenu", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern IntPtr GetSystemMenu(IntPtr hWnd, [MarshalAs(UnmanagedType.Bool)]bool bRevert);

            [DllImport("User32.dll", EntryPoint = "AppendMenu", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int AppendMenu(IntPtr hMenu, uint uFlags, IntPtr uIDNewItem, IntPtr lpNewItem);

            [DllImport("User32.dll", EntryPoint = "InsertMenu", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int InsertMenu(IntPtr hMenu, uint uPosition, uint uFlags, IntPtr uIDNewItem, IntPtr lpNewItem);

            [DllImport("User32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

            [DllImport("User32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

            [DllImport("User32.dll", EntryPoint = "GetWindowRect", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);

            [DllImport("User32.dll", EntryPoint = "GetClientRect", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);

            [DllImport("User32.dll", EntryPoint = "AdjustWindowRectEx", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int AdjustWindowRectEx(ref RECT lpRect, uint dwStyle, int bMenu, uint dwExStyle);

            [DllImport("User32.dll", EntryPoint = "GetWindowPlacement", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

            [DllImport("User32.dll", EntryPoint = "SetWindowPlacement", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);


        }

        public static class Gdi32
        {
            [DllImport("Gdi32.dll", EntryPoint = "SelectClipRgn", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int SelectClipRgn(IntPtr hdc, IntPtr hrgn);

            [DllImport("Gdi32.dll", EntryPoint = "GetDeviceCaps", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);


        }

        public static class Ole32
        {
            [DllImport("Ole32.dll", EntryPoint = "CreateILockBytesOnHGlobal", CharSet = CharSet.Auto, ExactSpelling = false, PreserveSig = false)]
            public static extern ILockBytes CreateILockBytesOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease);

            [DllImport("Ole32.dll", EntryPoint = "StgCreateDocfileOnILockBytes", CharSet = CharSet.Auto, ExactSpelling = false, PreserveSig = false)]
            public static extern IStorage StgCreateDocfileOnILockBytes(ILockBytes plkbyt, uint grfMode, uint reserved);

            [DllImport("Ole32.dll", EntryPoint = "OleSetContainedObject", CharSet = CharSet.Auto, ExactSpelling = false)]
            public static extern int OleSetContainedObject([MarshalAs(UnmanagedType.IUnknown)] object pUnk, bool fContained);


        }

        public static class GdiPlus
        {
            [DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)]
            public extern static int GdipMeasureDriverString(
                IntPtr graphics,
                string text,
                int length,
                IntPtr font,
                PointF[] positions,
                DriverStringOptions flags,
                IntPtr matrix,
                out RectangleF boundingBox);

            [DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)]
            public extern static int GdipDrawDriverString(
                IntPtr graphics,
                string text,
                int length,
                IntPtr font,
                IntPtr brush,
                PointF[] positions,
                DriverStringOptions flags,
                IntPtr matrix);


        }
    }
}
